//
//  SISSPikerView.m
//  HomeTest
//
//  Created by 优房网 on 2017/4/14.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPickerView.h"
#import "SISSAreaModel.h"

#define SISSPickerViewHeight 216
@interface SISSPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UIView *headView;
@property (strong, nonatomic) UIPickerView *pickerView;
/**
 titleLabel
 */
@property (weak, nonatomic) UILabel *titleLabel;


/** 区域 */
@property (strong, nonatomic) NSArray<SISSAreaModel *> *areas;

@property (nonatomic,assign) NSInteger firstCurrentIndex;//第一行当前位置
@property (nonatomic,assign) NSInteger secondCurrentIndex;//第二行当前位置

@end
@implementation SISSPickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self =  [super initWithFrame:frame];
    if (self) {
        [self internalConfig];
        
        
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self internalConfig];
    }
    return self;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    
    self.titleLabel.text = title;
}

- (void)setFirstIndex:(NSInteger)firstIndex {
    _firstIndex = firstIndex;
    
    if (_isArea) {
        [self.pickerView selectRow:_firstIndex inComponent:0 animated:NO];
    } else {
        [self.pickerView selectRow:_firstIndex inComponent:0 animated:NO];
    }
    self.firstCurrentIndex  = _firstIndex;
    [self.pickerView reloadAllComponents];
    [self show];
}

- (void)setSecondIndex:(NSInteger)secondIndex {
    _secondIndex = secondIndex;
    
    if (_isArea) {
        [self.pickerView selectRow:_secondIndex inComponent:1 animated:NO];
        self.secondCurrentIndex = _secondIndex;
        
        [self.pickerView reloadAllComponents];
    }
}


- (void)internalConfig {
    
    _backView = [[UIView alloc] initWithFrame:self.frame];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.5;
    [self addSubview:_backView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [_backView addGestureRecognizer:tap];
    
    self.backgroundColor = [UIColor clearColor];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height + 44, self.frame.size.width, SISSPickerViewHeight)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.pickerView];
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, self.pickerView.frame.origin.y - 43.5, self.frame.size.width, 43.5)];
    self.headView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 0, 43.5, 43.5);
    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:button];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.text = @"@";
    [self.headView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.headView);
    }];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(self.frame.size.width - 42, 0, 40, 43.5);
    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(completionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:button];
    
    [self addSubview:self.headView];
    
//    [self show];
}

- (void)show {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    self.hidden = NO;
    _backView.alpha = 0.6;
    [UIView animateWithDuration:0.5 animations:^{
        self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - SISSPickerViewHeight, self.frame.size.width, SISSPickerViewHeight);
        self.headView.frame = CGRectMake(0, self.pickerView.frame.origin.y - 43.5, self.frame.size.width, 43.5);
    }];
}

- (void)hide {
    _backView.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.pickerView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height+44, self.frame.size.width, SISSPickerViewHeight);
        self.headView.frame = CGRectMake(0, self.pickerView.frame.origin.y, self.frame.size.width, 43.5);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)completionButtonAction:(UIButton *)sender {
    
    NSString *firstTitle = @"";
    NSString *secondTitle = @"";
    
    if (self.isArea) {
        firstTitle = self.areas[self.firstCurrentIndex].name;
        if (self.areas[self.firstCurrentIndex].areas.count > 0) {
             secondTitle =  self.areas[self.firstCurrentIndex].areas[self.secondCurrentIndex];
        }
        secondTitle = [secondTitle isEqualToString:@"不限"] ? @"" : secondTitle;
    } else {
        firstTitle = self.titleArray[self.firstCurrentIndex];
        secondTitle = @"";
    }
    
    if (_completion) {
        _completion(firstTitle,secondTitle,self.firstCurrentIndex,self.secondCurrentIndex);
    }
    [self hide];
    
}

- (void)cancleButtonAction:(UIButton *)sender {
    [self hide];
}


#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate

//选项默认值
//- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated {
//
//}

//设置列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return self.isArea ? 2 : 1;
}


//返回数组总数
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    if (self.isArea) {
        return component == 0 ? self.areas.count : self.areas[self.firstCurrentIndex].areas.count;
    }
    return self.titleArray.count;
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (self.isArea) {
        return component == 0 ? self.areas[row].name : self.areas[self.firstCurrentIndex].areas[row];
    }
    return self.titleArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        self.firstCurrentIndex  = row;
        self.secondCurrentIndex = 0;
    } else {
        self.secondCurrentIndex = row;
    }
    
    [pickerView reloadAllComponents];
}

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    
//}

#pragma mark - 懒加载

- (NSArray<SISSAreaModel *> *)areas {
    
    if (!_areas) {
        // 去掉不限
        NSMutableArray<SISSAreaModel *> *arr = [SISSAreaModel loadAreaData].mutableCopy;
        NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:arr.count - 1];
        
        if (!self.firstContainUnlimited) {
            [arr removeObjectAtIndex:0];
            for (SISSAreaModel *model in arr) {
                SISSAreaModel *tempModel = [[SISSAreaModel alloc] init];
                NSMutableArray *areasTempArr = [NSMutableArray arrayWithArray:model.areas];
                [areasTempArr removeObjectAtIndex:0];
                tempModel.name = model.name;
                tempModel.areas = areasTempArr.copy;
                [tempArr addObject:tempModel];
            }
            _areas = tempArr.copy;
        } else {
            SISSAreaModel *tempModel = [[SISSAreaModel alloc] init];
             tempModel.name = @"不限";
            [arr replaceObjectAtIndex:0 withObject:tempModel];
            _areas = arr.copy;
        }
    }
    return _areas;
}

@end
